package com.elderlytour.controller;

import com.elderlytour.model.Trip;
import com.elderlytour.model.TripDetail;
import com.elderlytour.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import jakarta.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Controller
public class TourController {

    private List<User> users = new ArrayList<>();

    @GetMapping("/")
    public String home(Model model) {
        model.addAttribute("title", "银龄乐游");
        return "index";
    }

    @GetMapping("/trips")
    public String trips(Model model) {
        model.addAttribute("title", "热门行程");
        model.addAttribute("trips", Arrays.asList(
                new Trip(1, "江南水乡三日游", "乌镇+西塘+周庄，慢节奏游览，含早午晚餐，每日10:00出发", "/images/VCG江南.jpg"),
                new Trip(2, "京郊红叶赏秋", "香山+颐和园+圆明园，配备轮椅租赁服务，随队医生全程陪同", "/images/VCG圆明园.jpg"),
                new Trip(3, "海滨休闲五日游", "青岛金沙滩+威海刘公岛，全程海景酒店，每日含海鲜特色餐", "/images/VCG小岛.jpg"),
                new Trip(4, "山城文化之旅", "重庆洪崖洞+武隆天坑，专业导游讲解，安排特色火锅体验", "/images/VCG重庆.jpg"),
                new Trip(5, "温泉养生之旅", "南京汤山+常州恐龙园，含温泉泡浴，中医理疗师驻场", "/images/VCG泉.jpg"),
                new Trip(6, "草原生态游", "张北草原+沽源天鹅湖，安排骑马体验，提供防蚊防晒用品", "/images/VCG草原.jpg")));
        return "trips";
    }

    @GetMapping("/trip-detail")
    public String tripDetail(@RequestParam Integer id, Model model) {
        // 根据id获取具体行程详情（示例数据）
        TripDetail tripDetail = getTripDetailById(id); 
        model.addAttribute("title", "行程详情");
        model.addAttribute("tripDetail", tripDetail);
        return "trip-detail";
    }

    private TripDetail getTripDetailById(Integer id) {
        // 根据不同id返回对应行程详情
        switch(id) {
            case 1:
                return new TripDetail(id, "江南水乡三日游", "3天2晚", 
                    "D1: 08:00集合出发-12:00抵达乌镇（含午餐）-14:00游览东栅景区（含讲解）；D2: 09:00西塘古镇全天游览（含游船体验）-19:00夜游烟雨长廊（赠特色糕点）；D3: 09:00周庄古镇-12:00午餐-15:00返程",
                    "含3早餐+4正餐（江南风味）、53座空调旅游大巴（带USB充电）、景区首道门票+游船票、随队医护1名（每日量血压）",
                    "70岁以上需家属陪同并签署健康确认书；建议携带身份证、轻便衣物、防滑鞋、常用药品",
                    "/images/VCG江南.jpg");
            case 2:
                return new TripDetail(id, "京郊红叶赏秋", "2天1晚", 
                    "D1: 07:30集合-09:30抵达香山-11:00游览香山公园-13:00午餐-15:00游览颐和园-17:30返程；D2: 07:30集合-09:30抵达圆明园-11:00游览圆明园遗址-13:00午餐-15:00返程",
                    "含景区门票+轮椅租赁+随队医生+导游讲解",
                    "建议穿着防滑鞋、携带常用药品、佩戴口罩",
                    "/images/VCG圆明园.jpg");
            case 3:  // 新增海滨五日游详情
                return new TripDetail(id, "海滨休闲五日游", "5天4晚",
                    "D1: 09:00集合出发-14:00抵达青岛-游览金沙滩；D2: 崂山一日游；D3: 威海刘公岛；D4: 自由活动；D5: 返程",
                    "全程海景酒店+海鲜特色餐+旅游保险",
                    "建议携带防晒用品、泳衣泳帽",
                    "/images/VCG小岛.jpg");
            case 4:  // 新增山城文化之旅详情
                return new TripDetail(id, "山城文化之旅", "4天3晚",
                    "D1: 抵达重庆-洪崖洞夜景；D2: 武隆天坑；D3: 磁器口古镇；D4: 返程",
                    "特色火锅体验+专业导游讲解+防滑登山杖",
                    "建议穿着舒适登山鞋",
                    "/images/VCG重庆.jpg");
            case 5:  // 新增温泉养生之旅详情
                return new TripDetail(id, "温泉养生之旅", "3天2晚",
                    "D1: 南京汤山温泉；D2: 常州恐龙园；D3: 中医理疗体验",
                    "温泉酒店+中医问诊+理疗服务",
                    "泡温泉前需测量血压",
                    "/images/VCG泉.jpg");
            case 6:  // 新增草原生态游详情
                return new TripDetail(id, "草原生态游", "4天3晚",
                    "D1: 抵达张北草原；D2: 骑马体验；D3: 天鹅湖观鸟；D4: 返程",
                    "蒙古包住宿+防蚊防晒用品+随队医生",
                    "早晚温差大需带外套",
                    "/images/VCG草原.jpg");
            default:
                return new TripDetail(id, "默认行程", "", "", "", "", "/images/default.jpg");
        }
    }

    @GetMapping("/contact")
    public String contact(Model model) {
        model.addAttribute("title", "联系我们");
        model.addAttribute("address", "北京市朝阳区老年路18号");
        model.addAttribute("phone", "400-123-4567");
        return "contact";
    }

    @GetMapping("/register")
    public String showRegistrationForm(Model model) {
        model.addAttribute("title", "注册");
        return "register";
    }

    @PostMapping("/register")
    public String processRegistration(@RequestParam String username, @RequestParam String password, @RequestParam String phoneNumber, Model model) {
        // 检查用户名是否已存在
        for (User user : users) {
            if (user.getUsername().equals(username)) {
                model.addAttribute("message", "用户名已存在，请选择其他用户名。");
                return "register";
            }
        }
        User newUser = new User(username, password, phoneNumber);
        users.add(newUser);
        model.addAttribute("message", "注册成功！请登录。");
        return "login";
    }

    @GetMapping("/login")
    public String showLoginForm(Model model) {
        model.addAttribute("title", "登录");
        return "login";
    }

    @PostMapping("/login")
    public String processLogin(@RequestParam String username, @RequestParam String password, Model model, HttpSession session) {
        for (User user : users) {
            if (user.getUsername().equals(username) && user.getPassword().equals(password)) {
                // 登录成功，将用户信息存入会话
                session.setAttribute("user", user);
                model.addAttribute("message", "登录成功！欢迎您，" + username);
                return "index";
            }
        }
        model.addAttribute("message", "用户名或密码错误，请重新输入。");
        return "login";
    }

    @GetMapping("/logout")
    public String logout(HttpSession session) {
        // 销毁会话
        session.invalidate();
        return "index";
    }

    @PostMapping("/signup")
    public String signup(@RequestParam Integer tripId, HttpSession session, Model model) {
        // 检查用户是否登录
        User user = (User) session.getAttribute("user");
        if (user == null) {
            model.addAttribute("message", "请先登录再进行报名");
            return "login";
        }
        
        // 获取行程详情并添加到模型
        TripDetail tripDetail = getTripDetailById(tripId);
        model.addAttribute("tripDetail", tripDetail);
        model.addAttribute("title", "行程详情");
        model.addAttribute("successMessage", "报名成功！我们将尽快与您联系");
        return "trip-detail";
    }
}